package com.pzhuweb.sgblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhuweb.sgblog.domain.dto.TagListDto;
import com.pzhuweb.sgblog.domain.entity.Tag;
import com.pzhuweb.sgblog.domain.utils.BeanCopyUtils;
import com.pzhuweb.sgblog.domain.utils.ResponseResult;
import com.pzhuweb.sgblog.domain.vo.PageVo;
import com.pzhuweb.sgblog.domain.vo.TagVo;
import com.pzhuweb.sgblog.mapper.TagMapper;
import com.pzhuweb.sgblog.service.TagService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 鄧浩奇
 * @classname TagServiceImpl
 * @description
 * @since 2023/8/13 16:39
 **/

@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public ResponseResult<?> getTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        // 分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());
        queryWrapper.like(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());
        Page<Tag> tagPage = new Page<>();
        tagPage.setCurrent(pageNum);
        tagPage.setSize(pageSize);
        page(tagPage, queryWrapper);
        // 封装返回
        PageVo pageVo = new PageVo(tagPage.getRecords(), tagPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    /*
     * 新增标签
     * */
    @Override
    public ResponseResult<?> tag(TagListDto tagListDto) {
        Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
        save(tag);
        return ResponseResult.okResult();
    }

    /*
     * 删除标签
     * */
    @Override
    public ResponseResult<?> deleteTags(List<String> ids) {
        // 将id转换为Long类型
        List<Long> list = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        // 批量删除
        removeBatchByIds(list);
        return ResponseResult.okResult();
    }


    /*
     * 获取标签信息
     * */
    @Override
    public ResponseResult<?> getTagInfo(long tagId) {
        Tag tag = getById(tagId);
        return ResponseResult.okResult(new TagVo(tag.getId(), tag.getName(), tag.getRemark()));
    }

    /*
     * 修改标签
     * */
    @Override
    public ResponseResult<?> updateTag(TagVo tagVo) {
        LambdaUpdateWrapper<Tag> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Tag::getId, tagVo.getId())
                .set(Tag::getName, tagVo.getName())
                .set(Tag::getRemark, tagVo.getRemark());
        update(wrapper);
        return ResponseResult.okResult();
    }


    /*
    * 获取标签
    * */
    @Override
    public ResponseResult<?> listAllTag() {
        List<Tag> list = list();
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return ResponseResult.okResult(tagVos);
    }
}
